package com.liu.socket;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

@SuppressWarnings({"all"})
public class SocketTcpClient02 {
    public static void main(String[] args) throws IOException {
        //链接服务器
        Socket socket = new Socket(InetAddress.getLocalHost(), 9999);
        System.out.println("链接成功");
        System.out.println(socket.getClass());
        //得到和socket关联的输出流对象
        OutputStream outputStream = socket.getOutputStream();
        outputStream.write("hello,world".getBytes());
        //结束标记
        socket.shutdownOutput();
        InputStream inputStream = socket.getInputStream();
        byte[] bytes = new byte[1024];
        int readLen = 0;
        while ((readLen = inputStream.read(bytes)) != -1) {
            System.out.println(new String(bytes,0,readLen));
        }
        //关闭流和socket
        outputStream.close();
        socket.close();
        inputStream.close();
        System.out.println("客户端退出");
    }
}
